/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package xmarti.vacaxm.client.sequence;



import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import xmarti.vacaxm.action.ImplSequence;
import xmarti.vacaxm.beans.sequence.State;
import xmarti.vacaxm.exception.ExceptionHibernateVacaxm;
import xmarti.vacaxm.exception.ExceptionXML;
import xmarti.vacaxm.utils.JDialogXM;
import xmarti.vacaxm.utils.TDSLanguageUtils;
import xmarti.vacaxm.utils.TableModel;

/**
 *
 * @author xmarti
 */
public class JDState extends JDialogXM{    
    private List<State>states;
    private State state;
    private ImplSequence aSequence;
    private JPStateData jPData;
    public JDState(){        
        super(null,true,"JdState",TDSLanguageUtils.getMessage("jDialogState.JDialog.caption"));        
        this.aSequence=new ImplSequence(); 
        this.jPData= new JPStateData();
        super.getjPanelData().setViewportView(this.jPData);
        try {
            super.chargeTable();
        } catch (ExceptionHibernateVacaxm ex) {
            Logger.getLogger(JDState.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ExceptionXML ex) {
            Logger.getLogger(JDState.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
    }

    @Override
    public TableModel getModel() throws ExceptionHibernateVacaxm {
       TableModel dadesTaula = null;                
            this.states=(List<State>)this.aSequence.getStates(); 
            String [] columns={TDSLanguageUtils.getMessage("jDialogState.JTable.name.caption"),
                               TDSLanguageUtils.getMessage("jDialogState.JTable.duration.caption"),
                          TDSLanguageUtils.getMessage("jDialogState.JTable.description.caption"),                                                  
                          };    
            Object[][] dades=null;
            if (this.states.size()==0){
                dades=new Object[1][columns.length];                          
                dades[0][0]=new State();
                dades[0][1]="-";
                dades[0][2]="-";
                
            }
            else{
                dades=new Object[this.states.size()][columns.length];                          
                Integer i=0;            
                for (Iterator it = this.states.iterator(); it.hasNext();) {
                    State c = (State) it.next();                
                    dades[i][0]= c;
                    
                    dades[i][1]=c.getDuration();
                    if (c.getDescription()==(null))dades[i][2]="-"; else dades[i][2]= c.getDescription();                    
                    i++;
                }     
            }                  
        return new TableModel(columns, dades);   
    }

    @Override
    public void getData() {
        this.state.setName(this.jPData.getNameState());
        this.state.setDescription(this.jPData.getDescription());
        this.state.setDuration(this.jPData.getDuration());
    }

    @Override
    public void setData() {
       if(super.isInsert()) this.state=new State();
       else this.state=(State) super.getObject();
       this.jPData.setNameState(this.state.getName());
       this.jPData.setDescription(this.state.getDescription());
       this.jPData.setDuration(this.state.getDuration());              
    }

    @Override
    public void saveData() throws ExceptionHibernateVacaxm {
        getData();              
        if (this.state.getName().length()>0){
            if (super.isInsert()){
                this.state.setId(this.aSequence.add(this.state));
                super.setObject(this.state);                
            }else{
                this.aSequence.update(this.state);                
            }            
        }else{
            throw new ExceptionHibernateVacaxm(ExceptionHibernateVacaxm.ERR_INSERT_NAME_NULL);
        }
    }
    @Override
    public void removeData() throws ExceptionHibernateVacaxm {
        this.aSequence.delete(this.state);
        super.setObject(new State());
    }

    @Override
    public boolean isSaved() {        
        if(this.state==null) return true;
        if (!this.state.getName().equals(this.jPData.getNameState())) return false;
        if(this.state.getDuration()!=this.jPData.getDuration()) return false;
        if(!this.state.getDescription().equals(this.jPData.getDescription())) return false; 
        return true;
    }
    
}
